var $table = $("#manifestTab");
$(function () {
    initTable();
    //查询事件
    $("#queryManifestBtn").click(function () {
        $table.bootstrapTable('refresh');
    });
    
    //增加按钮
    $("#addBut").click(function () {
    	editManifestInfo(null);
    });
   
});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        showColumns: true, 
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryParams,
        url: '../manifest/getManifestByPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
            {
                title: "序号",
                align: 'center',
                formatter:function(value,row,index){
                    return index + 1;
                }
            },
            {
                title: "货单号",
                field: "manifestNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "货单名",
                field: "manifestName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "路线途经城市",
                field: "roadCrossName",
                align: 'center', 
                valign: 'middle',
                valign: 'middle'
            },
            {
                title: "货车牌号",
                field: "truckCarNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "司机姓名",
                field: "driverName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "开始站点",
                field: "stationStrName",
                align: 'center',
                valign: 'middle',
                valign: 'middle'
            },
            {
                title: "结束站点",
                field: "stationEndName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "是否提单",
                field: "manifestIsTake",
                align: 'center',
                formatter: function(value, row, index){
                    var take = "-";
                    if(row.manifestIsTake == 0){
                    	take = '<span style="color:#ed5565;">未提单</span>';
                    }else if(row.manifestIsTake == 1){
                    	take ='<span style="color:#23c6c8;">已提单</span>';
                    }
                    return take;
                }
            },
            {
                title: "回单状态",
                field: "manifestIsReturn",
                align: 'center',
                formatter: function(value, row, index){
                    var me = "-";
                    if(row.manifestIsReturn == 0){
                    	me = '<span style="color:#ed5565;">未回单</span>';
                    }else if(row.manifestIsReturn == 1){
                    	me ='<span style="color:#23c6c8;">已回单</span>';
                    }
                    return me;
                }
            },
            {
                title: "插入时间",
                field: "manifestInsTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                	var c = '<button class="btn btn-xs btn-info" type="button" onclick="manageOrder(\'' + row.manifestId + '\',\'' + row.manifestNum + '\')"><i class="fa fa-edit"></i> 管理订单</button> ';
                    var e = '<button class="btn btn-xs btn-info" type="button" onclick="editManifestInfo(\'' + row.manifestId + '\')"><i class="fa fa-edit"></i> 编辑</button> ';
                    var d = '';
                    if (row.manifestIsDelete == 0){
                        d = '<button class="btn btn-xs btn-danger" type="button" onclick="deleteManifestInfo(\'' + row.manifestId + '\',\'' + row.manifestNum + '\')"><i class="fa fa-trash-o"></i> 删除 </button> ';
                    }
                    return c + e + d;
                }
            }
        ],
        onLoadSuccess: function(data){
        }
    });
}

//分页参数，条件查询时的参数
function queryParams(queryParamsType){
	console.log('manifest.....:');
	var temp = {
		limit: queryParamsType.limit,   //页面大小
		offset: queryParamsType.offset,  //页码
		manifestNum: $.trim($('#manifestNum').val()),
		manifestIsTake: $.trim($('#manifestIsTake').val()),
		manifestIsReturn: $.trim($('#manifestIsReturn').val()),
	};
	return temp;
}

//管理订单
function manageOrder(manifestId, manifestNum){
	var index = layer.open({
        type: 1,
        title: '管理订单',
//        maxmin: false,	//禁用最大化或最小化按钮
        shadeClose: false, //点击遮罩关闭层
        area : ['1000px' , '700px'],
        content: '../manifest/manager_order.htm?id='+manifestId + "&num=" + manifestNum,
        success: function(layero, index){
        }
    });
	layer.full(index);
}


//编辑和增加货单
function editManifestInfo(id){
    var title;
    if(id){
        title = '编辑货单';
    }else{
        title = '新增货单';
    }
    layer.open({
        type: 2,
        title: title,
        maxmin: true,
        shadeClose: false, //点击遮罩关闭层
        area : ['500px' , '500px'],
        content: '../manifest/edit_manifest.htm?id='+id,
        success: function(layero, index){
        }
    });
}

//删除货单
function deleteManifestInfo(manifestNum) {
    layer.confirm('你确定要删除【' + manifestNum + '】货单吗？', {
        btn: ['修改', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "../manifest/delManifestInfo.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {'manifestNum': manifestNum},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '../manifest/getManifestByPage.htm'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}
